/*
 * @(#)XfslLogDialog.java        2003/01/01
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfsl.view;

import xfuzzy.util.*;
import xfuzzy.xfsl.model.XfslLog;
import xfuzzy.xfsl.view.listener.*;

import javax.swing.*;
import java.awt.*;
import java.io.*;

public class XfslLogDialog extends JDialog {

	//----------------------------------------------------------------------------//
	//                            COSTANTES PRIVADAS                              //
	//----------------------------------------------------------------------------//

	/**
	 * C�digo asociado a la clase serializable
	 */
	private static final long serialVersionUID = 95505666603069L;

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Ventana principal de la herramienta xfsl
	 */
	private Xfsl xfsl;
	
	/**
	 * Configuraci�n del fichero hist�rico
	 */
	private XfslLog logfile;
	
	/**
	 * Botones de selecci�n de las variables a almacenar
	 */
	private JRadioButton radio[];
	
	/**
	 * Campo de introducci�n del nombre del fichero
	 */
	private XTextForm text;
	
	/**
	 * Fichero seleccionado
	 */
	private File file;
	
	/**
	 * Flag para indicar que la selecci�n se ha efectuado
	 */
	private boolean result = false;

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public XfslLogDialog(Xfsl xfsl, XfslLog logfile) {
		super(xfsl,"Xfsl",true);

		this.xfsl = xfsl;
		this.logfile = logfile;
		this.file = logfile.getFile();
		build();
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Informa si se ha selecionado el fichero o se ha cancelado
	 */
	public boolean isSelected() {
		return result;
	}

	/**
	 * Selecciona el fichero y las variables a almacenar
	 */
	public void set() {
		if (file == null) {
			Toolkit.getDefaultToolkit().beep();
			return;
		}
		logfile.setFile(file);
		boolean[] log = new boolean[radio.length];
		for (int i = 0; i < radio.length; i++)
			log[i] = radio[i].isSelected();
		logfile.setSelection(log);
		result = true;
		setVisible(false);
	}

	/**
	 * Deselecciona el fichero de log
	 */
	public void unset() {
		logfile.setFile(null);
		result = true;
		setVisible(false);
	}

	/**
	 * Selecciona el fichero de almacenamiento
	 */
	public void file() {
		File wdir = (file == null ? xfsl.getWorkingDirectory() : file);
		XFileChooser chooser = new XFileChooser(wdir, XFileChooser.SELECT);
		chooser.setTitle("Select logfile");
		int val = chooser.showDialog();
		if (val == XFileChooser.CANCELED)
			return;
		file = chooser.getSelectedFile();
		text.setText(file.getAbsolutePath());
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Genera el contenido del di�logo
	 */
	private void build() {
		XfslLogActionListener listener = new XfslLogActionListener(this);
		
		text = new XTextForm("File",listener);
		text.setActionCommand("File");
		text.setEditable(false);
		if(file != null) text.setText(file.getAbsolutePath());

		String rname[] = { "Iteration", "Training Error", "Training RMSE",
				"Training MxAE", "Training Variation", "Test Error", "Test RMSE",
				"Test MxAE", "Test Variation" };

		radio = new JRadioButton[rname.length];
		for(int i=0; i<rname.length; i++)
			radio[i] = new JRadioButton(rname[i]);

		boolean[] log = logfile.getSelection();
		if(log != null)
			for(int i=0; i<radio.length && i<log.length; i++)
				radio[i].setSelected(log[i]);

		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout( (radio.length+1)/2,2 ));
		panel.add(radio[0]);
		panel.add(new JLabel(" "));
		panel.add(radio[1]);
		panel.add(radio[5]);
		panel.add(radio[2]);
		panel.add(radio[6]);
		panel.add(radio[3]);
		panel.add(radio[7]);
		panel.add(radio[4]);
		panel.add(radio[8]);
		Box panelbox = new Box(BoxLayout.X_AXIS);
		panelbox.add(Box.createHorizontalStrut(10));
		panelbox.add(Box.createHorizontalGlue());
		panelbox.add(panel);
		panelbox.add(Box.createHorizontalGlue());
		panelbox.add(Box.createHorizontalStrut(10));

		String lb[] = { "Set", "Unset", "Cancel" };
		XCommandForm form = new XCommandForm(lb,lb, listener );
		form.setCommandWidth(100);

		Container content = getContentPane();
		content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
		content.add(new XLabel("Log file"));
		content.add(text);
		content.add(new XLabel("Variables to log"));
		content.add(panelbox);
		content.add(form);

		Point loc = xfsl.getLocationOnScreen();
		loc.x += 40; loc.y += 200;
		this.setLocation(loc);
		pack();
	}
}
